package org.haredot.config;

import org.haredot.filter.BearerTokenAuthenticationFilter;
import org.haredot.filter.JWTAuthenticationFilter;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {

    @Resource
    private JwtToken jwtToken;

    @Resource
    public UserDetailsService userDetailsService ;

    @Resource
    private RabbitTemplate rabbitTemplate ;

    @Bean
    AuthenticationManager authenticationManager() {
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setUserDetailsService(this.userDetailsService);
        daoAuthenticationProvider.setPasswordEncoder(this.passwordEncoder());
        ProviderManager providerManager = new ProviderManager(daoAuthenticationProvider);
        // 系统自带的 User 类 在认证成功后，默认会去掉 password 敏感信息， 如果希望保持 password ,则 设置为false
        providerManager.setEraseCredentialsAfterAuthentication(false);
        return providerManager;
    }

    @Bean
    public JWTAuthenticationFilter jwtAuthenticationFilter() {
        JWTAuthenticationFilter jwtAuthenticationFilter = new JWTAuthenticationFilter(jwtToken, this.authenticationManager());
        jwtAuthenticationFilter.setUserDetailsService(this.userDetailsService);
        jwtAuthenticationFilter.setRabbitTemplate(rabbitTemplate);
        return jwtAuthenticationFilter;
    }


    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public BearerTokenAuthenticationFilter bearerTokenAuthenticationFilter() {
        return new BearerTokenAuthenticationFilter(jwtToken, this.userDetailsService) ;
    }


    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

        // 添加需要授权的资源
        http.authorizeRequests()
                .anyRequest()
                .authenticated();

        // 禁用 csrf 跨站资源保护
        http.csrf().disable();

        // 设置跨域访问
        http.cors();

        // 将 表单登录 禁用
        http.formLogin().disable();

        // 添加自定义过滤器
        http.addFilterBefore(this.jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
        http.addFilterBefore(this.bearerTokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

        http.addFilterBefore(new OrderedFormContentFilter(), UsernamePasswordAuthenticationFilter.class);


        return http.build();
    }

}
